﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nollie
{
	public class CacheItem
	{
		public string Key
		{
			get;
			set;
		}
		public object Value
		{
			get;
			set;
		}
		public DateTime ExpiryDate
		{
			get;
			set;
		}
		/// <summary>
		/// List of one or more keys that can be used
		/// </summary>
		public TagList Tags
		{
			get;
			set;
		}

		public CacheItem()
		{
			Tags = new TagList();
		}
		public CacheItem(string key, object value, TagList tags, DateTime expiryDate)
		{
			this.Key = key;
			this.Value = value;
			this.Tags = tags ?? new TagList();
			this.ExpiryDate = expiryDate;

			Validate();
		}

		/// <summary>
		/// Validates and throws an exception if any of the properties contains invalid values.
		/// </summary>
		public void Validate()
		{
			if (string.IsNullOrEmpty(this.Key))
				throw new NollieException("Key cannot be null or empty.");

			if (this.Value == null)
				throw new NollieException("Value cannot be null or empty.");

			if (IsExpired())
				throw new NollieException("ExpiryDate cannot be in the past.");
		}
		/// <summary>
		/// Returns true if the expiry date is in the past.
		/// </summary>
		/// <returns></returns>
		public bool IsExpired()
		{
			if (ExpiryDate < DateTime.Now)
				return true;

			return false;
		}
	}
}
